package com.eipc.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.eipc.R;
import com.eipc.model.Game;
import com.eipc.model.Weather;

public class BottomBar extends LinearLayout {
	public BottomBar(Context context){
		super(context);
		mContext = context;
		initView();
	}
	
	public BottomBar(Context context,AttributeSet attrs){
		super(context, attrs);
		mContext = context;
		initView();
	}

	private void initView(){
		LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		LinearLayout layout = (LinearLayout)inflater.inflate(R.layout.bottom_bar, null);
		this.addView(layout);
		mOne = (ViewFlipper)layout.findViewById(R.id.one);
		mOther = (ViewFlipper)layout.findViewById(R.id.other);
		mCall = (ImageButton)layout.findViewById(R.id.call);
		mSMS = (ImageButton)layout.findViewById(R.id.sms);
		mConstacts = (ImageButton)layout.findViewById(R.id.constacts);
		mMode = (CheckBox)layout.findViewById(R.id.mode);
		mWeather = (TextView)layout.findViewById(R.id.tv_weather);	
		mWeatherImage = (ImageView)layout.findViewById(R.id.iv_weather);	
	}
	
	public void setWeather(Weather weather){
		if(weather!=null){
				mWeather.setText(mContext.getString(R.string.temperature, weather.temperature));
				mWeatherImage.setImageResource(getWeatherImage(weather.currentStatus));
		}
	}
	
	/*
	 * 根据类型获取图片
	 */
	private int getWeatherImage(String type){
		int image = 0;
		image = mContext.getResources().getIdentifier("home_weather_"+(type.length()>1?type:"0"+type), "drawable", mContext.getPackageName());
		return image;
	}
	
	public void setMode(boolean checked){
		mMode.setChecked(checked);
	}
	
	public void setGame(Game game){
		if(game!=null){
			mOne.addView(getScoreView(game.one,false));
			mOne.addView(getScoreView(game.oneScore,false));
			mOther.addView(getScoreView(game.other,false));
			mOther.addView(getScoreView(game.otherScore,false));
		}
		else{
			mOne.addView(getScoreView("0",true));
			mOther.addView(getScoreView("0",true));
		}
	}
	
	private View getScoreView(String txt,boolean flag){
		TextView view = (TextView)((LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.simple_textview, null);
		view.setText(txt);
		if(flag){
			view.setTextColor(android.R.color.black);
		}
		return view;
	}
	
	public void startFlipper(){
		mOne.startFlipping();
		mOther.startFlipping();
	}
	
	public void stopFlipper(){
		if(mOne.isFlipping()) mOne.stopFlipping();
		if(mOther.isFlipping()) mOther.stopFlipping();
	}
	
	
	public void setCallClickListener(OnClickListener listener){
		if(listener!=null){
			mCall.setOnClickListener(listener);
		}
	}
	
	public void setSMSClickListener(OnClickListener listener){
		if(listener!=null){
			mSMS.setOnClickListener(listener);
		}
	}
	
	public void setConstactslickListener(OnClickListener listener){
		if(listener!=null){
			mConstacts.setOnClickListener(listener);
		}
	}
	
	public void setModeClickListener(OnCheckedChangeListener listener){
		if(listener!=null){
			mMode.setOnCheckedChangeListener(listener);
		}
	}
	
	
	private Context mContext;
	private ViewFlipper mOne;
	private ViewFlipper mOther;
	private ImageButton mCall;
	private ImageButton mSMS;
	private ImageButton mConstacts;
	private CheckBox mMode;
	private TextView mWeather;
	private ImageView mWeatherImage;
}